/**
 * Copyright (C) 2016 GridSafe, Inc.
 * @author Neo Ko <neo@grid-safe.com>
 */

"use strict";

var isSubmit = false;
var isSubmit2 = false;

function showChargeRuleDialog() {
  $("#charging-rule").show();
}

function hideChargeRuleDialog() {
  $("#charging-rule").hide();
}

function showHint() {
  $('#hint').show();
}

function hideHint() {
  $('#hint').hide();
}

function getOpenid() {
  return $('#openid').val();
}

function getProinfo(chmb, is_disable) {
  var is_disable = false;
  if (!chmb) {
    is_disable = true;
    chmb = "15900000000";
  }

  resetshow();
  hideHint();

  if (chmb.length != 11) {
    return
  }

  $.getJSON('/api/v1/getproduct/', {
    phone: chmb,
    b: $("#b").val()
  }, function (data) {
    if (data.state) {
      var count = data.count;
      var flows = data.flows;

      if (count < 1) {
        $("#speclist").html("没有可用的流量包");
        $("#box-all").show();
        return;
      }

      var item;
      var container = [];
      flows.sort(function (prevValue, value) {
        return prevValue.price - value.price;
      });

      var nowDate = new Date().getDate();

      if (!is_disable) {
        for (var i = 0, flowLen = flows.length; i < flowLen; i++) {
          item = flows[i];
          if (item.status == 1) {
            var ele = createCellEle(item);
            container.push(ele)
          } else {
            var elem = createDisabledCellEle(item);
            container.push(elem)
          }
        }
      } else {
        for (var i = 0, flowLen = flows.length; i < flowLen; i++) {
          item = flows[i];
          var elem = createDisabledCellEle(item);
          container.push(elem)
        }
        showHint();
      }
      $("#flow_plans > *").remove();
      $("#flow_plans").append(container);
    }
    setStyle();
  });
}

function getDaysOfMonth() {
  var d = new Date();
  var result = new Date(d.getYear(), d.getMonth() + 1, 0);
  return result.getDate();
}

function createCellEle(item) {
  var openId = getOpenid();
  return $("<div class='weui_cell'>" +
    "<div class='weui_cell_hd weui_cell_primary'>" +
    "<div>" + item.name + "</div>" +
    "<div><small style='color: #c5c5c5'>" + item.description + "</small></div>" +
    "</div>" +
    "<div class='weui_cell_ft'>" +
    "<a class='weui_btn weui_btn_mini weui_btn_plain_primary fixed_width_btn' " +
    "onclick='fillFormParamsWrapper(\"" +
    openId + "\", \"" + item.name + "\", \"" + item.price + "\", \"" + item.carrier +
    "\")'>" + item.price + "元" + "</a>" + "</div>" + "</div>");
}

function createDisabledCellEle(item) {
  return $("<div class='weui_cell'>" +
    "<div class='weui_cell_hd weui_cell_primary'>" +
    "<div>" + item.name + "</div>" +
    "<div><small style='color: #c5c5c5'>" + item.description + "</small></div>" +
    "</div>" +
    "<div class='weui_cell_ft'>" +
    "<a class='weui_btn weui_btn_disabled weui_btn_mini weui_btn_default fixed_width_btn' " +
    "onclick=''>" + item.price + "元" + "</a>" + "</div>" + "</div>");
}

function fillFormParams(productName, productPrice) {
  if (!isSubmit) {
    isSubmit = true; // prevent multiple submit
    $('#product-name-field').val(productName);
    $('#product-price-field').val(productPrice);
    $('#charge_form').submit();
    isSubmit = false;
  }
}

function fillFormParamsWrapper(openId, productName, productPrice, carrier) {
  if (!isSubmit2) {
    isSubmit2 = true; // prevent multiple submit
    var planId = productName;
    var data_plan_price = productPrice;
    var data_plan_size = productName;
    statistics_init(openId, location.href);
    var callback = fillFormParams.bind(null, productName, productPrice);
    statistics_data_plan_pay(planId, data_plan_size, data_plan_price, carrier, callback, 3000);
    isSubmit2 = false;
  }
}

function resetshow() {
  $("#prompt").hide();
  $("#speclist").html("");
  $("#speclistp").html("");
  $("#boxprv").hide();
  $("#product-id-field").val("");
  $("#userprice").html("--");
  $("#mbinfo").text("");
  $("#flow_plans").html("");
}


function setStyle() {
  $(".list > li").click(function () {
    $(".list > li").removeClass("cur");
    $(this).addClass("cur");
    $("#userprice").html($(this).attr("pr"));

    var btnEle = $("#payment-submit-btn");
    btnEle.removeClass('weui_btn_disabled');
    btnEle.removeClass('weui_btn_default');
    btnEle.addClass('weui_btn_primary');
    btnEle.text("立刻支付");

    // very important!!!
    // copy selected traffic plan id to hidden input field
    $("#product-id-field").val($(this).attr("idr"));
  });
}

$(document).ready(function () {
  setStyle();
  var phone_num = $('#chmb').val();
  getProinfo(phone_num);
});
